#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 5;
const int M = 1e9 + 7;

int t, n, k;
ll a[N];

ll f(ll i, ll j)
{
    return i * j - (ll)k * (a[i] | a[j]);
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
            cin >> a[i];

        ll res = f(n - 1, n);
        for (int i = max(1, n - 2 * k); i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                res = max(res, f(i, j));
            }
        }

        cout << res << endl;
    }
    return 0;
}